'''
Created on Oct 11, 2012

@author: daeheehan
'''
from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
import sys

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = 'luispigu'
GITHUB_USER_ID_4 = 'skswhwo'

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = "wasd1234"

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID, password=GITHUB_USER_PW)
print gh.ratelimit_remaining, 'request remaining'

# Github Bootstrap Repository information
GITHUB_USER_NAME = 'robbyrussell'
GITHUB_REPOSITORY_NAME = 'oh-my-zsh'

# Bootstrap contributors information file
CONTRIBUTORS_WATCH_TIME_FILE = 'oh_my_zsh_contributors_watch_time.txt'

CONTRIBUTORS_COMMIT_NUM_WATCH_EVNET_FILE = 'oh_my_zsh_contributors_commit_num_watch_event.txt'

contributors_watch_time_dict = {}
contributors_watch_result_dict = {}

try:
    # Github Bootstrap project contributors list from files
    f_collaborators = open(CONTRIBUTORS_WATCH_TIME_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[0].strip()
            str_watch_time = tuple_list[1].strip()
            watch_time = dateutil.parser.parse(str_watch_time)
            print watch_time, type(watch_time)
            contributors_watch_time_dict[login] = watch_time

    f_collaborators.close()

    print 'loading watch_time_contributors file is finished'
    repo = gh.repository(GITHUB_USER_NAME, GITHUB_REPOSITORY_NAME)
        
    for watcher, watchevent_time in contributors_watch_time_dict.items():
        sys.stdout.write(watcher + ", " + str(watchevent_time) + '\n')
        
        print 'login id: ' +  watcher
        commit_number_before = 0
        commit_number_after = 0

        try:
            for repo_commit in repo.iter_commits(author=watcher, number = -1):
                # Checking what if there is no commit history
                raw_commit_date = repo_commit.commit.author['date']
                commit_date = dateutil.parser.parse(raw_commit_date)
                print watcher, watchevent_time, commit_date

                if commit_date > watchevent_time:
                   commit_number_after = commit_number_after + 1
                else:
                    commit_number_before = commit_number_before + 1  
                
                if(gh.ratelimit_remaining < 100):
                    time.sleep(60 * 60)
            
            str_before_after = str(commit_number_before) + ', '+ str(commit_number_after)
            contributors_watch_result_dict[watcher] = str_before_after
            
        except Exception as e:
            print 'The inner Exception', e.args[0]
            continue
#        break
#
    f_contributors_commit_num = open(CONTRIBUTORS_COMMIT_NUM_WATCH_EVNET_FILE, 'w')
    print 'Printing dictionary: watch'
    for key, value in contributors_watch_result_dict.items():
        sys.stdout.write(key + ", " + str(value) + '\n')
        f_contributors_commit_num.write(key + ", " + str(value) + '\n')

    f_contributors_commit_num.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

#    pprint(vars(sub))
print gh.ratelimit_remaining,'request remaining'

print
print 'Finally the end'